#include "StdAfx.h"
#include "AR.h"


CAR::CAR(void)
{
	m_attack = 0.05;
	m_release = 0.05;
	m_duration = 0.5;
	m_time = 0.0;
}


CAR::~CAR(void)
{
}

void CAR::Start()
{
    m_time = 0.;
}

bool CAR::Generate()
{
	if (m_time <= m_attack)
	{
		double attack = (m_time / m_attack);
		m_frame[0] = m_source->Frame(0) * attack;
		m_frame[1] = m_source->Frame(1) * attack;
	}
	else if (m_time >= m_duration*(1./(m_bpm/60.)) - m_release)
	{
		double release = ((m_duration*(1./(m_bpm/60.)) - m_time) / m_release);
		m_frame[0] = m_source->Frame(0) * release;
		m_frame[1] = m_source->Frame(1) * release;
	}
	else
	{
		m_frame[0] = m_source->Frame(0);
		m_frame[1] = m_source->Frame(1);
	}

	// Update time
    m_time += GetSamplePeriod();

    return m_time < m_duration*(1./(m_bpm/60.));
}